<!DOCTYPE html>

<html>
<head>
  <title>twitter.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="configure.html">
                configure.coffee
              </a>
            
              
              <a class="source" href="dataParser.html">
                dataParser.coffee
              </a>
            
              
              <a class="source" href="database.html">
                database.coffee
              </a>
            
              
              <a class="source" href="logger.html">
                logger.coffee
              </a>
            
              
              <a class="source" href="mail.html">
                mail.coffee
              </a>
            
              
              <a class="source" href="messages.html">
                messages.coffee
              </a>
            
              
              <a class="source" href="routes.html">
                routes.coffee
              </a>
            
              
              <a class="source" href="settings.html">
                settings.coffee
              </a>
            
              
              <a class="source" href="settings.default.html">
                settings.default.coffee
              </a>
            
              
              <a class="source" href="twitter.html">
                twitter.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>twitter.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h2>SERVER TWITTER</h2>

            </div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Handles communications with Twitter.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">Twitter</span></span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Require modules.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    logger = require <span class="string">"./logger.coffee"</span>
    moment = require <span class="string">"moment"</span>
    settings = require <span class="string">"./settings.coffee"</span>
    twitter = require <span class="string">"ntwitter"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>The twitter handler.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    twit = <span class="literal">null</span></pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>Will be true only if it validates with the supplied the credentials.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    auth = <span class="literal">false</span></pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <h2>INIT</h2>

            </div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>Init the Twitter handler, but only if the consumer and access keys were
properly set on the <a href="settings.html">settings</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    init: (retry) =&gt;
        retry = <span class="number">0</span> <span class="keyword">if</span> <span class="keyword">not</span> retry?

        <span class="keyword">if</span> settings.Twitter.consumerSecret? <span class="keyword">and</span> settings.Twitter.accessSecret? <span class="keyword">and</span> settings.Twitter.accessSecret <span class="keyword">isnt</span> <span class="string">""</span>
            keys =
                consumer_key: settings.Twitter.consumerKey,
                consumer_secret: settings.Twitter.consumerSecret,
                access_token_key: settings.Twitter.accessToken,
                access_token_secret: settings.Twitter.accessSecret

            twit = <span class="keyword">new</span> twitter keys</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Verify if credentials are valid and set the <code>auth</code> variable.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            twit.verifyCredentials (err, data) =&gt;</pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>If fails, log and set a timeout to try again in a few seconds.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>                <span class="keyword">if</span> err?
                    logger.warn <span class="string">"Twitter.init"</span>, <span class="string">"Can't verify credentials."</span>, err
                    auth = <span class="literal">false</span>
                    setTimeout (() -&gt; <span class="property">@init</span> retry + <span class="number">1</span>), settings.Twitter.retryInterval * <span class="number">1000</span>
                <span class="keyword">else</span>
                    logger.info <span class="string">"Twitter.init"</span>, <span class="string">"Authorized with ID <span class="subst">#{data.id}</span>."</span>
                    auth = <span class="literal">true</span></pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <h2>POST STATUS</h2>

            </div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>Post a single countdown to Twitter, but only if Twitter
credentials were validated before, so <code>auth</code> must be true.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    postCountdown: (cd) =&gt;
        logTitle = <span class="string">"Twitter.postCountdown"</span>

        <span class="keyword">if</span> <span class="keyword">not</span> cd?
            <span class="keyword">if</span> settings.General.debug
                logger.warn logTitle, <span class="string">"Countdown is null or undefined. ABORT!"</span>
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p>Cancel if countdown is private.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> cd.getBool <span class="string">"isPrivate"</span>
            <span class="keyword">if</span> settings.General.debug
                logger.warn logTitle, <span class="string">"Will NOT post Countdown <span class="subst">#{friendlyUrl}</span> to Twitter because it is set as private."</span>
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>Get the its friendly URL.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        friendlyUrl = cd.get <span class="string">"friendlyUrl"</span>

        <span class="property">@logNoAuth</span> logTitle, <span class="string">"Will NOT post Countdown <span class="subst">#{friendlyUrl}</span>."</span> <span class="keyword">if</span> <span class="keyword">not</span> auth</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>Get all necessary details and create the message.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        title = cd.get <span class="string">"title"</span>
        creatorName = cd.get <span class="string">"creatorName"</span>
        targetDate = cd.get <span class="string">"targetDate"</span>
        targetDate = moment targetDate
        message = <span class="string">"<span class="subst">#{title}</span> @ <span class="subst">#{targetDate.calendar()}</span>, by <span class="subst">#{creatorName}</span>."</span>
        link = <span class="string">"http://counting.to/<span class="subst">#{friendlyUrl}</span>"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>Post to Twitter.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        twit.updateStatus <span class="string">"<span class="subst">#{message}</span> <span class="subst">#{link}</span>"</span>, (err, data) =&gt;
            <span class="keyword">if</span> err?
                logger.warn logTitle, <span class="string">"Post FAILED: <span class="subst">#{message}</span>"</span>, err
            <span class="keyword">else</span> <span class="keyword">if</span> settings.General.debug
                logger.info logTitle, <span class="string">"Posted: <span class="subst">#{message}</span>"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <h2>MESSAGES</h2>

            </div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>Returns a list of recent messages since the specified ID, and process them
to generate new countdown models.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    processMessages: (sinceId) =&gt;
        logTitle = <span class="string">"Twitter.getRecentTweets"</span>
        <span class="property">@logNoAuth</span> logTitle, <span class="string">"Will NOT get recent messages."</span> <span class="keyword">if</span> <span class="keyword">not</span> auth</pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>Make a request to get direct messages.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        twit.getDirectMessages {<span class="string">"since_id"</span>: sinceId}, (err, data) =&gt;
            <span class="keyword">if</span> err?
                logger.warn logTitle, <span class="string">"Get since <span class="subst">#{sinceId}</span> FAILED."</span>, err
            <span class="keyword">else</span></pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>Process all messages. If message turns out to be a countdown,
destroy it after processing.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>                <span class="keyword">for</span> t <span class="keyword">in</span> data
                    <span class="property">@destroyMessage</span> t.id</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <p>Destroy the specified direct message.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    destroyMessage: (id) =&gt;
        logTitle = <span class="string">"Twitter.getRecentTweets"</span>
        <span class="property">@logNoAuth</span> logTitle, <span class="string">"Will NOT destroy message <span class="subst">#{id}</span>."</span> <span class="keyword">if</span> <span class="keyword">not</span> auth</pre></div></div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              <p>Make a request to destroy the specified message.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        twit.destroyDirectMessage id, (err, data) =&gt;
            <span class="keyword">if</span> err?
                <span class="keyword">return</span>
            <span class="keyword">else</span>
                <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-22">&#182;</a>
              </div>
              <h2>HELPER METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-23">&#182;</a>
              </div>
              <p>If <code>auth</code> is false and a request is made, log a warning saying it can&#39;t proceed.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    logNoAuth: (title, msg) =&gt;
        <span class="keyword">if</span> settings.General.debug
            logger.warn title, <span class="string">"No auth set!"</span>, msg
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-24">&#182;</a>
              </div>
              <h2>Singleton implementation</h2>

            </div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-25">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Twitter.<span class="function"><span class="title">getInstance</span></span> = -&gt;
    <span class="property">@instance</span> = <span class="keyword">new</span> Twitter() <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@instance</span>?
    <span class="keyword">return</span> <span class="property">@instance</span>

module.exports = exports = Twitter.getInstance()</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
